package com.example.helloandroid;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RemoteViews.ActionException;

public class HelloAndroid extends Activity {
	/** Called when the activity is first created. */

	private int resultat;
	private int[] fake_resultat = new int[3];
	
	/* button used to print random results */
	private Button button1;
	private Button button2;
	private Button button3;
	private Thread threadCounter;
	
	private TextView nbResultats;
		
	private int counter = 0;
	private int max_table;
	private int time = 0;
	private TextView textCounter ;
	
	private int DEFAULT_MAX_TABLE = 12;
	private int SLEEP_TIME = 1000;
	private int MAX_DEFAULT_SECOND_ = 10;
	private int NUMBER_OF_BUTTON =3;
	  
	
	
	
	protected int requestCode;
	
	Handler handler = new Handler() {
	
	public void handleMessage(Message msg) {
			{
				
				textCounter.setText(Integer.toString(time--));
				if (time ==0) {					  
					setResult(RESULT_OK, (new Intent()).setAction(String.valueOf(counter)));
		            finish();
				}
				
			
			}
			
		}
	};
	

	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setTitle("Table de multiplication");
		Intent intent = getIntent();
		max_table = intent.getIntExtra("MAX_TABLE",DEFAULT_MAX_TABLE);
		threadCounter = new Thread(new Runnable() {
			public void run() {
				try {
					while (true) {
						Thread.sleep(SLEEP_TIME);
						handler.sendMessage(handler.obtainMessage());
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		threadCounter.start();
		initScreen();
		//counter(); 
		
		textCounter = ((TextView) findViewById(R.id.counter));
		nbResultats = ((TextView) findViewById(R.id.nbResultats));
		nbResultats.setText("");
		
		button1.setOnClickListener(mButton1OnClickListener);
		button2.setOnClickListener(mButton2OnClickListener);
		button3.setOnClickListener(mButton3OnClickListener);
		//buttonInit.setOnClickListener(mButtonInitOnClickListener);

	}
	
	private void initScreen() {
		time = MAX_DEFAULT_SECOND_;		
		Random randomNumber = new Random();
		int number1 = randomNumber.nextInt(max_table);
		int number2 = randomNumber.nextInt(max_table);
		resultat = number1 * number2;
		String s = new String(Integer.toString(number1) + "x"
				+ Integer.toString(number2) + "= ?");
		// Set message to be appropriate for this screen.
		((TextView) findViewById(R.id.label)).setText(s);

		button1 = (Button) findViewById(R.id.valeur1);
		button2 = (Button) findViewById(R.id.valeur2);
		button3 = (Button) findViewById(R.id.valeur3);
		//buttonInit = (Button) findViewById(R.id.init);

		for (int i = 0; i < NUMBER_OF_BUTTON; i++) {
			fake_resultat[i] = randomNumber.nextInt(max_table*max_table);
		}

		fake_resultat[randomNumber.nextInt(NUMBER_OF_BUTTON)] = number1 * number2;
		button1.setText(String.valueOf(fake_resultat[0]));
		button2.setText(String.valueOf(fake_resultat[1]));
		button3.setText(String.valueOf(fake_resultat[2]));
		((TextView) findViewById(R.id.resultat)).setText("");
		
		
		
	}


	private OnClickListener mButton1OnClickListener = new OnClickListener() {
		public void onClick(View v) {

			// TODO Auto-generated method stub

			if (resultat == (fake_resultat[0])) {
				//((TextView) findViewById(R.id.resultat)).setText("Bravo");
				counter++;
				nbResultats.setText(Integer.toString(counter));
				initScreen();
			} else {
				//((TextView) findViewById(R.id.resultat)).setText("Faux");
			}
		

		}
	};

	private OnClickListener mButton2OnClickListener = new OnClickListener() {
		public void onClick(View v) {

			// TODO Auto-generated method stub

			if (resultat == (fake_resultat[1])) {
				//((TextView) findViewById(R.id.resultat)).setText("Bravo");
				counter++;
				nbResultats.setText(Integer.toString(counter));
				initScreen();
				
			} else {
				//((TextView) findViewById(R.id.resultat)).setText("Faux");
			}
		

		}
	};
	private OnClickListener mButton3OnClickListener = new OnClickListener() {
		public void onClick(View v) {

			// TODO Auto-generated method stub

			if (resultat == (fake_resultat[2])) {
				//((TextView) findViewById(R.id.resultat)).setText("Bravo");
				counter++;
				nbResultats.setText(Integer.toString(counter));
				initScreen();
			} else {
				//((TextView) findViewById(R.id.resultat)).setText("Faux");
			}
		
		}
	

	};

	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		threadCounter.interrupt();
		setResult(RESULT_OK, (new Intent()).setAction(String.valueOf(counter)));
		finish();
 		super.onBackPressed();
		
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		try {
			threadCounter.interrupt();
		}catch (Exception e)
		{
			e.printStackTrace();
		}
		super.onPause();
	}
	 
};
